package com.cloud.wms.ui.sort.fragment;

import android.os.Bundle;
import android.os.Message;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.utils.NumberUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseViewPagerFragment;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.sort.contract.OrderSortContract;
import com.cloud.wms.ui.sort.entity.BindInfoEntity;
import com.cloud.wms.ui.sort.entity.SortCommodityEntity;
import com.cloud.wms.ui.sort.presenter.OrderSortPresenter;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.trello.rxlifecycle3.LifecycleTransformer;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-03-07 16:10
 * @Description:
 */
public class OrderSortDetailFragment extends BaseViewPagerFragment<OrderSortPresenter> implements OrderSortContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private BaseQuickAdapter<SortCommodityEntity, BaseViewHolder> mAdapter;
    private BindInfoEntity bindInfoEntity;
    private TaskEntity taskEntity;
    private boolean isComplete;

    @Override
    protected int getResId() {
        return R.layout.fragment_common_detail;
    }

    @Override
    protected void setupComponent() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initEvent(Bundle bundle) {
        bindInfoEntity = getArguments().getParcelable("SortInfoEntity");
        taskEntity = getArguments().getParcelable("Task");
        isComplete = getArguments().getBoolean("IsComplete");

        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new BaseQuickAdapter<SortCommodityEntity, BaseViewHolder>(R.layout.item_common_sort_detail) {
            @Override
            protected void convert(BaseViewHolder helper, SortCommodityEntity item) {
                helper.setText(R.id.tv_commodity_code, item.getBarCode())
                        .setText(R.id.tv_commodity_name, item.getCommodityName())
                        .setText(R.id.tv_pos_code, item.getPosCode())
                        .setText(R.id.tv_commodity_amount, (isComplete ? item.getAmount() : item.getAmount()) + "")
                        .setText(R.id.tv_sort_state, isComplete ? "已完成" : "待拣选")
                        .setText(R.id.tv_amount_name, isComplete ? "已拣选数" : "待拣选数")
                        .setTextColor(R.id.tv_sort_state, ContextCompat.getColor(getContext(), isComplete ? R.color.font_hint : R.color.font_green));
            }
        };
        rvList.setItemAnimator(new DefaultItemAnimator());
        rvList.setAdapter(mAdapter);
        mPresenter.loadSortDetail(taskEntity.getMainBillID(), taskEntity.getBillID(), isComplete);

        refresh.setOnRefreshListener(refreshLayout -> mPresenter.loadSortDetail(taskEntity.getMainBillID(), taskEntity.getBillID(), isComplete));
    }

    @Override
    protected void initData() {

    }

    @Override
    public void showEmpty() {
        if (refresh.isRefreshing())
            refresh.finishRefresh();
        mAdapter.getData().clear();
        mAdapter.notifyDataSetChanged();
        mAdapter.setEmptyView(getEmptyDataView());
    }

    @Override
    public void showError() {
        mAdapter.setEmptyView(getErrorView());
        if (refresh.isRefreshing())
            refresh.finishRefresh();
    }

    private View getEmptyDataView() {
        View notDataView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_empty, rvList, false);
        notDataView.setOnClickListener(v -> mPresenter.loadSortDetail(taskEntity.getMainBillID(), taskEntity.getBillID(), isComplete));
        return notDataView;
    }

    private View getErrorView() {
        View errorView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_error, rvList, false);
        errorView.setOnClickListener(v -> mPresenter.loadSortDetail(taskEntity.getMainBillID(), taskEntity.getBillID(), isComplete));
        return errorView;
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case OrderSortPresenter.SORT_DETAIL:
                refresh.finishRefresh();
                mAdapter.setNewInstance(((ArrayList<SortCommodityEntity>) message.obj));
                break;
        }
    }

    @Override
    public void info(String msg) {
        super.info(msg);
        refresh.finishRefresh();
    }

    @Override
    public void error(String msg) {
        super.error(msg);
        refresh.finishRefresh();
    }

    @Override
    public <R> LifecycleTransformer<R> toLifecycle() {
        return bindToLifecycle();
    }

    public static BaseViewPagerFragment newInstance(Bundle bundle) {
        OrderSortDetailFragment fragment = new OrderSortDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
